package v1

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"io/ioutil"
	"os/exec"
	"strconv"
	"strings"
	"time"
)

// @Tags SysLicence
// @Summary 创建SysLicence
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysLicence true "创建SysLicence"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sysLicence/createSysLicence [post]
func CreateSysLicence(c *gin.Context) {
	var sysLicence model.SysLicence
	_ = c.ShouldBindJSON(&sysLicence)
	genCmd := "/var/lib/chanpin/generateLicence " + strings.TrimSpace(sysLicence.LicenceUUID) + " " + strconv.Itoa(sysLicence.LicenceDuration) + " " + strconv.Itoa(sysLicence.LicencePoints)
	licencePath := strings.TrimSpace(sysLicence.LicenceUUID) + "." + time.Now().Format("20060102150405")
	mvCmd := "mv /var/lib/chanpin/hlas.license." + strings.TrimSpace(sysLicence.LicenceUUID) + " /var/lib/chanpin/licence/" + licencePath
	execShell(genCmd)
	execShell(mvCmd)
	sysLicence.LicencePath = licencePath
	// 赋值操作用户
	_, user := service.FindUserById(utils.GetUserIdByRequest(*c))
	sysLicence.LicenceAuthor = user.Username + "(" + user.NickName + ")"
	if err := service.CreateSysLicence(sysLicence); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {

		response.OkWithMessage("创建成功", c)
	}
}

// @Tags SysLicence
// @Summary 删除SysLicence
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysLicence true "删除SysLicence"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /sysLicence/deleteSysLicence [delete]
func DeleteSysLicence(c *gin.Context) {
	var sysLicence model.SysLicence
	_ = c.ShouldBindJSON(&sysLicence)
	if err := service.DeleteSysLicence(sysLicence); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags SysLicence
// @Summary 批量删除SysLicence
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除SysLicence"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /sysLicence/deleteSysLicenceByIds [delete]
func DeleteSysLicenceByIds(c *gin.Context) {
	var IDS request.IdsReq
	_ = c.ShouldBindJSON(&IDS)
	if err := service.DeleteSysLicenceByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// @Tags SysLicence
// @Summary 更新SysLicence
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysLicence true "更新SysLicence"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /sysLicence/updateSysLicence [put]
func UpdateSysLicence(c *gin.Context) {
	var sysLicence model.SysLicence
	_ = c.ShouldBindJSON(&sysLicence)
	if err := service.UpdateSysLicence(sysLicence); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags SysLicence
// @Summary 用id查询SysLicence
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.SysLicence true "用id查询SysLicence"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /sysLicence/findSysLicence [get]
func FindSysLicence(c *gin.Context) {
	var sysLicence model.SysLicence
	_ = c.ShouldBindQuery(&sysLicence)
	if err, resysLicence := service.GetSysLicence(sysLicence.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"resysLicence": resysLicence}, c)
	}
}

// @Tags SysLicence
// @Summary 分页获取SysLicence列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.SysLicenceSearch true "分页获取SysLicence列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /sysLicence/getSysLicenceList [get]
func GetSysLicenceList(c *gin.Context) {
	var pageInfo request.SysLicenceSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetSysLicenceInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败", zap.Any("err", err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}

func execShell(cmdStr string) {
	global.GVA_LOG.Info(cmdStr)
	cmd := exec.Command("/bin/bash", "-c", cmdStr)

	//创建获取命令输出管道
	stdout, err := cmd.StdoutPipe()
	if err != nil {
		global.GVA_LOG.Error("Error:can not obtain stdout pipe for command:" + err.Error())
	}

	//执行命令
	if err := cmd.Start(); err != nil {
		global.GVA_LOG.Error("Error:The command is err:" + err.Error())
	}

	//读取所有输出
	bytes, err := ioutil.ReadAll(stdout)
	if err != nil {
		global.GVA_LOG.Error("ReadAll Stdout:" + err.Error())
	}

	if err := cmd.Wait(); err != nil {
		global.GVA_LOG.Error("wait:" + err.Error())
	}
	global.GVA_LOG.Info("stdout:\n" + string(bytes))
}
